$(function () {

    var client_datagrid =  $("#client_datagrid");
    var client_dialog = $("#client_dialog");
    var client_form = $("#client_form");
    var client_toolbar = $("#client_toolbar")
    var clientQuery_dialog = $("#clientQuery_dialog");
    var clientQuery_form = $("#clientQuery_form")
    var introducerStudent = $("#introducerStudent");
    var stuTk_dialog = $("#stuTk_dialog");
    var clientTraceAdd_form = $("#clientTraceAdd_form");
    var enroll_dialog = $("#enroll_dialog")
    var tabsForm=$("#tabsForm");
    var exam_dialog = $("#exam_dialog");

    //给 a链接统一绑定事件
    $("a[data-cmd]").click(function(){
        var methodName = $(this).data("cmd");
        //调用方法
        methodObj[methodName]();
    });

    introducerStudent.combobox({
        url : "/potentialStudent/queryIntroducerStudent.do",
        queryParams: {
            "state" : "正常状态"
        }
    });

    client_datagrid.datagrid({
        url: '/potentialStudent/query.do',
        fit: true,
        width: 1200,
        fitColumns: true,
        rownumbers: true,
        columns: [[
            {field: 'name', title: '姓名', width: 30,align:'center'},
            {field: 'salesman', title: '营销人员', width: 30,align:'center',
                formatter: function(value,row,index){
                   return row.salesman.username;
                }
            },
            {field: 'trackingTimes', title: '跟踪次数', width: 30,align:'center'},
            {field: 'finalTrackingTime', title: '最后跟踪时间', width: 50,align:'center'},
            {field: 'visitTime', title: '约访时间', width: 50,align:'center'},
            {field: 'followUpTime', title: '下次跟进时间', width: 50,align:'center'},
            {field: 'qq', title: 'QQ', width: 30,align:'center'},
            {field: 'tel', title: '电话', width: 30,align:'center'},
            {field: 'school', title: '学校', width: 30,align:'center'},
            {field: 'intentionDegree', title: '意向程度', width: 30,align:'center'},
            {field: 'campus', title: '意向校区', width: 30,align:'center'},
            {field: 'grade', title: '意向班级', width: 30,align:'center',
                formatter: function(value,row,index){
                    if (value != null) {
                        return row.grade.className;
                    }
                }},
            {
                field: 'state',
                title: '状态',
                width: 30,align:'center',
                formatter: function (value) {
                    return value =="正常" ? '<span style="color: red">'+value+'</span>' : '<span style="color: green">'+value+'</span>'
                }
            },
            {
                field: 'isFollowUp',
                title: '是否跟进',
                width: 30,
                align:'center',
                formatter: function (value) {
                    return value ? '<span style="color: grey">跟进</span>' : '<span style="color: red">未跟进</span>'
                }
            },
            {field: 'comment', title: '备注', width: 30,align:'center'}
        ]],
        toolbar: '#client_toolbar',
        singleSelect: true,
        rownumbers: true,
        pagination: true
    });

    client_dialog.dialog({
        width: 720,
        height: 450,
        closed: true,
        modal:true,
        draggable:true,
        buttons: [
            {
                text: '保存',
                iconCls: 'icon-save',
                handler: function () {
                    methodObj.save();
                }
            },
            {
                text: '取消',
                iconCls: 'icon-cancel',
                handler: function () {
                    methodObj.cancel();
                }
            }
        ],
        //清空表单数据
        onClose: function () {
            client_form.form('clear');
        }
    });

    clientQuery_dialog.dialog({
        width: 720,
        height: 400,
        title: "高级查询窗口",
        closed: true,
        resizable: true,
        modal:true,
        draggable:true,
        buttons: [
            {
                text: '提交',
                iconCls: 'icon-save',
                handler: function () {
                    methodObj.submitForm();
                }
            },
            {
                text: '取消',
                iconCls: 'icon-cancel',
                handler: function () {
                    methodObj.cancelQuery();
                }
            }
        ],
        //清空表单数据
        onClose: function () {
            clientQuery_form.form('clear');
        }
    });

    //跟踪弹窗
    stuTk_dialog.dialog({
        width: 650,
        height: 400,
        closed: true,
        resizable: false,
        buttons: [
            {
                text: '保存',
                iconCls: 'icon-save',
                handler: function () {
                    methodObj.saveTrace();
                }
            },
            {
                text: '取消',
                iconCls: 'icon-cancel',
                handler: function () {
                    methodObj.cancelTrace();
                }
            }
        ],
        //清空表单数据
        onClose: function () {
            $("#clientTraceAdd_form").form('clear');
        }
    });

    //学员转正详细信息弹窗
    enroll_dialog.dialog({
        width: 720,
        height: 450,
        closed: true,
        resizable: false,
        buttons: [
            {
                text: '保存',
                iconCls: 'icon-save',
                handler: function () {
                    methodObj.saveEnroll();
                }
            },
            {
                text: '取消',
                iconCls: 'icon-cancel',
                handler: function () {
                    methodObj.cancleEnroll();
                }
            }
        ],
        //清空表单数据
        onClose: function () {
            tabsForm.form('clear');
        }
    });

    //学员考试登记
    exam_dialog.dialog({
        width: 400,
        height: 300,
        closed: true,
        resizable: false,
        buttons: [
            {
                text: '保存',
                iconCls: 'icon-save',
                handler: function () {
                    methodObj.saveExam();
                }
            },
            {
                text: '取消',
                iconCls: 'icon-cancel',
                handler: function () {
                    methodObj.cancelExam();
                }
            }
        ],
        //清空表单数据
        onClose: function () {
            $("#exam_form").form('clear');
        }
    });

    //使用一个对象来统一管理实践的方法
    var methodObj = {
        //添加
        add: function () {
            client_form.find("input").prop("readonly",false);
            $(".easyui-combobox").combobox({
                readonly:false
            });
            //弹出表格窗口
            client_dialog.dialog("open")

            //设置标题
            client_dialog.dialog('setTitle', '新增潜在学员');
        },

        //编辑
        edit: function () {
            //判断是否选中了数据
            var row = client_datagrid.datagrid('getSelected');
            if (!row) {
                //如果没有选中 提示框提示信息
                $.messager.alert("温馨提示", "请选中一条数据", "warning");
                return;
            }

            //选中了一条数据 回显表单数据(根据同名匹配原则 row对象中的属性名要与form中name相同才可以回显)
            if (row.introducerStudent) {
                row["introducerStudent.id"] = row.introducerStudent.id;
            }
            if (row.grade) {
                row["grade.id"] = row.grade.id;
            }
            if (row.client) {
                row["client.id"] = row.client.id;
            }

            var yes_radio = $("#yes_radio");

            var no_radio = $("#no_radio");

            $("#client_form input").prop("readonly",false);

            //delete row.isPay;
            client_form.form('load', row);

            if (yes_radio[0].value == row.isPay) {
                yes_radio.prop("checked",true);
            }
            if (no_radio[0].value == row.isPay) {
                no_radio.prop("checked",true);
            }

            //弹出表格窗口
            client_dialog.dialog("open");

            //设置标题
            client_dialog.dialog('setTitle', '编辑潜在学员');
        },

        //保存操作
        save: function () {
            client_form.form('submit', {
                url: '/potentialStudent/saveOrUpdate.do',
                success: function (data) {
                    //转换为json对象
                    data = $.parseJSON(data);
                    if (data.success) {
                        //提示用户操作结果
                        $.messager.alert('温馨提示', '保存成功!', 'info', function () {
                            //关闭弹出框
                            methodObj.cancel();
                            // load 加载和显示第一页的所有行
                            client_datagrid.datagrid('reload');
                        });
                    } else {
                        $.messager.alert('温馨提示', data.msg, 'error')
                    }
                }
            })
        },

        //取消操作
        cancel: function () {
            client_dialog.dialog('close');
        },

        //查看
        see: function () {
            methodObj.edit();
            $("#client_form input").prop("readonly",true);
            $("#client_form").form("disableValidation");
            client_dialog.dialog("setTitle","查看学员信息");
        },
        //取消操作
        cancelSee: function () {
            see_dialog.dialog('close');
        },

        //高级查询
        query:function () {
            //刷新
            client_datagrid.datagrid("load");
            clientQuery_form.find("input").prop("readonly",false);

            //弹出表格窗口
            clientQuery_dialog.dialog("open");

            //设置标题
            clientQuery_dialog.dialog('setTitle','高级查询窗口');
        },

        //提交高级查询
        submitForm:function () {
            clientQuery_form.form("submit", {
                url: '/potentialStudent/query.do',
                success: function (data) {
                    //转换为json对象
                    data = $.parseJSON(data);
                    clientQuery_dialog.dialog("close");
                    client_datagrid.datagrid("loadData", data);
                }
            });
        },

        //关闭高级查询窗口
        cancelQuery:function () {
            clientQuery_dialog.dialog('close');
        },

        //导出
        exportXls: function () {
            $("a[data-cmd='exportXls']").prop("href","/potentialStudent/exportXls.do");
        },

        //打开跟踪弹窗
        clientTrace: function () {
            //判断是否选中了数据
            var row = client_datagrid.datagrid('getSelected');
            if (!row) {
                //如果没有选中 提示框提示信息
                $.messager.alert("温馨提示", "请选中一条数据", "warning");
                return;
            }
            if (row.state=="正常状态") {
                //如果没有选中 提示框提示信息
                $.messager.alert("温馨提示", "已是正式学员", "warning");
                return;
            }
            // console.log(row);
            //选中了一条数据 回显表单数据(根据同名匹配原则 row对象中的属性名要与form中name相同才可以回显)
            row["potentialStudent.id"] = row.id;
            row["qqNum"] = row.qq;
            row["state.name"] = row.state;
            if (row.salesman) {
                row["consultant.id"] = row.salesman.id;
            }
            if (row.client) {
                row["school.id"] = row.client.id;
            }
            if (row.grade) {
                row["grade.id"] = row.grade.id;
            }
            row["appointTime"] = row.visitTime;
            row["wantedLevel.name"] = row.intentionDegree;
            row["nextFollowTime"] = row.followUpTime;
            row["focusProblem"] = row.question;
            row["remark"] = row.comment;
            clientTraceAdd_form.form('load', row);
            //弹出表格窗口
            stuTk_dialog.dialog("open");
            //设置标题
            stuTk_dialog.dialog('setTitle', '添加跟踪记录');
        },
        //保存操作
        saveTrace: function () {
            clientTraceAdd_form.form('submit', {
                url: '/studentTracking/saveOrUpdate.do',
                success: function (data) {
                    //转换为json对象
                    data = $.parseJSON(data);
                    if (data.success) {
                        //提示用户操作结果
                        $.messager.alert('温馨提示', '保存成功!', 'info', function () {
                            //关闭弹出框
                            methodObj.cancelTrace();
                            // load 加载和显示第一页的所有行
                            client_datagrid.datagrid('reload')
                        });
                        var id = $("#potentialStudentId").val();
                        console.log(id);
                        $.post("/potentialStudent/updateTracking.do",{id:id});
                    } else {
                        $.messager.alert('温馨提示', data.msg, 'error')
                    }
                }
            })
        },

        //取消操作
        cancelTrace: function () {
            stuTk_dialog.dialog('close');
        },

        //放入资源池
        abandon: function () {
            //判断是否选中了数据
            var row = client_datagrid.datagrid('getSelected');
            if (!row) {
                //如果没有选中 提示框提示信息
                $.messager.alert("温馨提示", "请选中一条数据", "warning");
                return;
            }
            if (row.state=="正常状态") {
                //如果没有选中 提示框提示信息
                $.messager.alert("温馨提示", "已是正式学员", "warning");
                return;
            }
            var id = row.id;
            $.post("/potentialStudent/putPoolState.do",{id:id},function (data) {
                if (data.success) {
                    //提示用户操作结果
                    $.messager.alert('温馨提示', '操作成功!', 'info', function () {
                        //关闭弹出框
                        methodObj.cancelTrace();
                        // load 加载和显示第一页的所有行
                        client_datagrid.datagrid('reload')
                    });
                } else {
                    $.messager.alert('温馨提示', data.msg, 'error')
                }
            });
        },

        //考试登记
        editExam: function () {
            //判断是否选中了数据
            var row = client_datagrid.datagrid('getSelected');
            if (!row) {
                //如果没有选中 提示框提示信息
                $.messager.alert("温馨提示", "请选中一条数据", "warning");
                return;
            };
            $(".easyui-textbox").textbox({
                readonly:true
            });

            //选中了一条数据 回显表单数据(根据同名匹配原则 row对象中的属性名要与form中name相同才可以回显)

            row["potentialStudent.id"]=row.id;
            row["qqNum"] = row.qq;
            if (row.grade) {
                row["grade.id"] =  row.grade.id;
            }
            $("#exam_form").form('load', row);

            //弹出表格窗口
            exam_dialog.dialog("open");

            //设置标题
            exam_dialog.dialog('setTitle', '编辑部门');
        },
        //保存操作
        saveExam: function () {
            $("#exam_form").form('submit', {
                url: '/examination/saveOrUpdate.do',
                success: function (data) {
                    if (data) {
                        //转换为json对象
                        data = $.parseJSON(data);
                        if (data.success) {
                            //提示用户操作结果
                            $.messager.alert('温馨提示', '保存成功!', 'info', function () {
                                //关闭弹出框
                                methodObj.cancelExam();
                                // load 加载和显示第一页的所有行
                                client_datagrid.datagrid('reload')
                            })
                        } else {
                            $.messager.alert('温馨提示', data.msg, 'error')
                        }
                    }
                }
            })
        },
        //取消操作
        cancelExam: function () {
            exam_dialog.dialog('close');
        },

        //转正--弹窗
        enroll: function () {
            //判断是否选中了数据
            var row = client_datagrid.datagrid('getSelected');
            if (!row) {
                //如果没有选中 提示框提示信息
                $.messager.alert("温馨提示", "请选中一条数据", "warning");
                return;
            }
             if (row.state!="预备状态") {
                 //如果没有选中 提示框提示信息
                 $.messager.alert("温馨提示", "已是正式学员", "warning");
                 return;
             }

            $("#client_form input").prop("readonly",true);

            //选中了一条数据 回显表单数据(根据同名匹配原则 row对象中的属性名要与form中name相同才可以回显)
            //row["iDNum"] = row.idnum;
            if(row.salesman){
                row["salesman.username"]=row.salesman.username;
            }
            if(row.grade){
                row["className"]=row.grade.className;
                row["grade.id"]=row.grade.id;
            }
            if (row.gathering) {
                /*收款金额*/
                if (!row.gathering.money) {
                    row.gathering.money = 0;
                    row["moneyState"] = false;
                    $(".moneyState").val("未缴费");
                }
                row["money"] = row.gathering.money;
                /*未缴金额*/
                if (!row.gathering.owe) {
                    row.gathering.owe = 0;
                }
                $(".moneyState").val("缴费");
                row["moneyState"] = true;
                row["owe"] = row.gathering.owe;
                row["totlMoney"] = row.gathering.money + row.gathering.owe;
                /*付款类型*/
                if (row.gathering.type) {
                    row["type"] = row.gathering.type;
                }
                /*收款时间*/
                if (row.gathering.time) {
                    row["finaTime"] = row.gathering.time;
                }
            }
            $(".money").find(".easyui-textbox").prop("readonly",true);
            tabsForm.form('load',row);
            //回显就业意向复选框的状态
            //var empIntention = row.employmentIntention;//["1","2"]
            // console.log(empIntention);
            //处理字符串->json
            /*console.log($("input[name='employmentIntention']"));
            console.log($("input[name='employmentIntention']")[0]);*/
            //因为row中有employmentIntention这个属性,会自动设置,应该先删除掉
            //delete row.employmentIntention;
            //tabsForm.form('load',row);
            /*if (empIntention) {
                var eis = $.parseJSON(empIntention);
                console.log(eis);
                if (eis) {
                    $.each(eis,function (i,e) {
                        $.each($(".employmentIntention"),function (ind,ele) {
                            var el = $(ele).val();
                            if(e == el){
                                $(ele).prop("checked",true);

                            }
                        });
                    })
                }
            }
*/
            //弹出表格窗口
            enroll_dialog.dialog("open");
            //设置标题
            enroll_dialog.dialog('setTitle', '转正编辑');
        },
        //保存转正信息
        saveEnroll:function () {
            tabsForm.form('submit', {
                url: '/potentialStudent/updateForEnroll.do',
                //url:"/studentmanagement/update.do",
                onSubmit:function (param) {
                    param["grade.id"]=$("#gradeId").combobox("getValue");
                },
                success: function (data) {
                    //转换为json对象
                    data = $.parseJSON(data);
                    if (data.success) {
                        //提示用户操作结果
                        $.messager.alert('温馨提示', '保存成功!', 'info', function () {
                            //关闭弹出框
                            methodObj.cancleEnroll();
                            // load 加载和显示第一页的所有行
                            client_datagrid.datagrid('reload')
                        });
                    } else {
                        $.messager.alert('温馨提示', data.msg, 'error')
                    }
                }
            });
        },
        //关闭窗口
        cancleEnroll:function () {
            enroll_dialog.dialog('close');
        },
        filter: function () {
            var state = $("a[data-state]").data("state");
            $.post("/potentialStudent/queryIntroducerStudent.do", {state: state}, function (data){
                client_datagrid.datagrid("loadData",data);
            });
        },
        reload: function () {
            client_datagrid.datagrid("load");
        },
    }
});

